/*
This file is part of Docking@Home.
http://docking.gcl.cis.udel.edu
Copyright (C) 2009 University of Delaware

Docking@Home is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Docking@Home is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Docking@Home.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Filename: ImageButton.h
	Description: A button in the UI that is represented by an image
	Revision History: 
		Created - 01/08/08
	Author: Robert Keller (01/08/08 -> current)
*/

#include "ImageButton.h"
#include "Button.h"

// ------------------------------------------------------------
// Constructor / Destructor
// ------------------------------------------------------------

ImageButton::ImageButton ()
{
	init("", 0.0f, 0.0f, 0.0f, 0.0f);
}// LowLevelTab

ImageButton::ImageButton (const char n[BUTTON_MAX_NAME_LENGTH])
{
	init(n, 0.0f, 0.0f, 0.0f, 0.0f);
}// LowLevelTab

ImageButton::ImageButton (const char n[BUTTON_MAX_NAME_LENGTH],	float xVal, float yVal)
{
	init(n, xVal, yVal, 0.0f, 0.0f);
}// LowLevelTab

ImageButton::ImageButton (const char n[BUTTON_MAX_NAME_LENGTH],	float xVal, float yVal, float w, float h)
{
	init(n, xVal, yVal, w, h);
}// LowLevelTab

ImageButton::~ImageButton ()
{
}// ~LowLevelTab

// ------------------------------------------------------------
// Initialization Functions
// ------------------------------------------------------------

void ImageButton::init (const char n[BUTTON_MAX_NAME_LENGTH],
	float xVal, float yVal, float w, float h)
{
	setName(n);
	
	x = xVal;
	y = yVal;

	width = w;
	height = h;
}// init

// ------------------------------------------------------------
// Member Modifiers
// ------------------------------------------------------------

void ImageButton::loadImage (
	const char path [BUTTONIMAGE_MAX_FILE_PATH_LENGTH])
{
	image.load_image_file(path);
}// loadImage

// ------------------------------------------------------------
// Required Button Functions
// ------------------------------------------------------------

// Name: determineState
// Description: Determines if the button has been either selected,
//	hovered over, or nothing. This does it by checking whether or
//	not the provided coordinates are within the boundaries of the
//	button and whether it has been clicked
// Parameters:
//	mouseX, x coordinates
//	mouseY, y coordinates
//	clicked, whether or not a button was clicked
// Returns: true if the button now is selected, false otherwise

bool ImageButton::determineState (int mouseX, int mouseY, int clicked)
{

	if (mouseX <= currX + currWidth 
			&& mouseX >= currX
			&& mouseY <= currY + currHeight
			&& mouseY >= currY)
	{
		// Button was selected
		if (clicked)
		{
			// Check if this action should be maintained after 
			// a single click
			if (bHoldSelection)
				bSelected = true;
			
			state = SELECTED;

			return true;
		}// if
		else
			state = HOVER;
	}// if
	else
	{
		(bSelected == true) ? state = SELECTED : state = UNSELECTED;
	}// else

	return false;

}// determineState

// Resizes all properties based on the scale factors maintaining 
// a specified percentage from the top and the left
void ImageButton::resize (float scaleX, float scaleY)
{
	currWidth = width * scaleX;
	currHeight = height * scaleY;

	currX = x * scaleX;
	currY = y * scaleY;

}// resize

// Maintains the same placement on the bottom of the screen.
//  - The image will be enlarged but the bottom pixels will 
//    be the same distance from the bottom of the screen
void ImageButton::resizeSameBottom (float scaleX, float scaleY,
	const int standardHeight, int windowHeight)
{
	currWidth = width * scaleX;
	currHeight = height * scaleY;

	currX = x * scaleX;
	currY = windowHeight - (standardHeight - y) - (currHeight - height);

}// resize

// Maintains the same placement on the left of the screen.
//  - The image will be enlarged but the left pixels will 
//    be the same distance from the left side of the screen
void ImageButton::resizeSameLeft (float scaleX, float scaleY,
	int standardWidth, int windowWidth)
{
	currWidth = width * scaleX;
	currHeight = height * scaleY;

	currX = x;
	currY = y * scaleY;

}// resizeSameLeft

void ImageButton::draw ()
{
	switch (state)
	{
	case UNSELECTED:
		drawUnselected();
		break;
	case SELECTED:
		drawSelected();
		break;
	case HOVER:
		drawHovered();
		break;
	}// switch
}// draw

void ImageButton::drawSelected ()
{
	if (image.present) 
	{	
		glHelper->drawTexturedQuadWH(currX, currY,
			currWidth, currHeight, 
			selectedColor, image.id);
    }// if 
}// draw

void ImageButton::drawUnselected ()
{
	if (image.present) 
	{	
		glHelper->drawTexturedQuadWH(currX, currY,
			currWidth, currHeight,
			unselectedColor, image.id);
    }// if 
}// draw

void ImageButton::drawHovered ()
{
	if (image.present) 
	{	
		glHelper->drawTexturedQuadWH(currX, currY, 
			currWidth, currHeight,
			hoverColor, image.id);
    }// if 
}// draw
